#include "RigidBody.h"
#include <math.h>

RigidBody::RigidBody(void)
{
	pos.SetPoints(0, 0, 0);

	rotation.SetPoints(0, 0, 0);
	speed.SetPoints(0, 0, 1);

}

void RigidBody::LoadModel(string s)
{
	m.LoadFile(s);
}

void RigidBody::SetBoundingBox(float width, float height, float depth, Point p)
{
    boundingBox.SetCentre((p));
    boundingBox.SetBoundary(width, height, depth);
}

void RigidBody::SetSpeed(float x, float y, float z)
{
	speed.SetPoints(x, y, z);
}

void RigidBody::SetPosition(float x, float y, float z)
{
	pos.SetPoints(x, y, z);
	//boundingBox.SetCentre(x, y, z);
}

void RigidBody::MoveForward(float num)
{
	Point dP = speed * num;
	pos = pos + dP;

	boundingBox.MoveForward(dP);
}

void RigidBody::Turn(float degree)
{
	rotation.y += degree;
	rotation.y = fmod(rotation.y, 360);
}

// dt is set to 0.01 second for simplicity
void RigidBody::Update(float dt)
{
	Point dPos = speed * dt;
	pos = pos + dPos;
}

// implement this function
bool RigidBody::IsColliding(RigidBody rigidBody)
{
	return false;
}

void RigidBody::DrawWireFrame()
{
	glPushMatrix();

    glTranslatef(pos.x, pos.y, pos.z);
	glRotatef(rotation.x, 1, 0, 0);
	glRotatef(rotation.y, 0, 1, 0);
	
    m.DrawWireFrame();
    
    glPopMatrix();
}

void RigidBody::DrawModel()
{
	glPushMatrix();

    glTranslatef(pos.x, pos.y, pos.z);
	glRotatef(rotation.x, 1, 0, 0);
	glRotatef(rotation.y, 0, 1, 0);

	m.DrawModel();
    
    glPopMatrix();
}

void RigidBody::DrawBoundingBox()
{
	glDisable(GL_LIGHTING);

	glPushMatrix();

		glColor3f(1, 0, 0);

		boundingBox.DrawBoundingBox();

		glColor3f(1, 1, 1);
	
	glPopMatrix();

	glEnable(GL_LIGHTING);
}